package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.TeachingUpdateParam;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.TeachingListResult;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.TeachingService;
import com.tcm.service.TermService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 教学管理控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/7/13 14:49
 */
@Slf4j
@RestController
@RequestMapping("/pc/teaching")
public class TeachingController {

    @Autowired
    private TeachingService teachingService;

    @Autowired
    private TermService termService;

    /**
     * 获取学期列表
     * @return 学期列表响应结果
     */
    @RequestMapping(value = "/term", method = RequestMethod.GET)
    public R getTermList(){
        return R.success(termService.getTermList());
    }

    /**
     * 添加教学计划
     * @param param 教学计划更新参数
     * @param request http请求对象
     * @return 响应添加结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addTeaching(@RequestBody @Valid TeachingUpdateParam param, HttpServletRequest request){
        boolean b = false;
        try {
            b = teachingService.addTeaching(param, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 修改教学计划
     * @param param 教学计划更新参数
     * @param request http请求对象
     * @return 响应添加结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyTeaching(@RequestBody @Valid TeachingUpdateParam param, HttpServletRequest request){
        boolean b = false;
        try {
            b = teachingService.modifyTeaching(param, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(ResponseEnum.PARAM_IS_INVAlID);
        }
        return b ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 删除教学计划
     * @param termId 学期ID
     * @param bookId 课程ID
     * @param teacherId 教师ID
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeTeaching(@RequestParam Long termId, @RequestParam Long bookId, @RequestParam Long teacherId, HttpServletRequest request){
        boolean b = false;
        try {
            b = teachingService.removeTeaching(termId, bookId, teacherId, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 获取教学计划并封装到指定格式结果集
     * @param termId 学期ID
     * @param bookId 课程ID，可选
     * @param teacherId 教师ID，可选
     * @param clazzId 班级ID，可选
     * @return 教学计划并封装到指定格式结果集
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getTeachingByFormat(@RequestParam Long termId,
                                 @RequestParam(required = false) Long bookId,
                                 @RequestParam(required = false) Long teacherId,
                                 @RequestParam(required = false) Long clazzId){
        List<TeachingListResult> teachingByFormat = teachingService.getTeachingByFormat(termId, bookId, teacherId, clazzId);
        return R.success(teachingByFormat);
    }

}
